home *** CD-ROM | disk | FTP | other *** search
- #include "Neural Network.h"
- #include <math.h>
-
- extern FILE * Jac;
-
- /*-------------------------
- Compute the next step of iteration by solving linear system.
- */
- OLSbyQRmethod(X,P,D,Y)
- DTypeMatrix * X; /* pointer to transpose of explanatory data */
- DTypeVector * P; /* pointer to Pi vector */
- DTypeVector * D; /* pointer to Diag vector */
- DTypeVector * Y; /* pointer to vector of dependent variables*/
- {
- int sing;
-
- sing = QRDecomposition(X,P,D);
- if(sing==FALSE)
- { ComputeQY(X,P,Y);
- SolveRbY(X,D,Y);
- }
- return(sing);
- }
-
-
- /*-------------------------
- Compute Q*Y to get dependent variable for triangularized system, where
- Q=U(N)*U(N-1)*...*U(1), is the product of N elementary reflecting matrices.
- Uses output from QR decomposition, see Stewert, p239.
- */
- ComputeQY(Q,P,Y)
- DTypeMatrix * Q; /* matrix output from QR decomposition */
- DTypeVector * P; /* vector of pi values from QR decomposition */
- DTypeVector * Y; /* vector of dependent values */
- {
- register int i;
- register int k;
- register int M;
- register int N;
- register DataType sum; /* used to temporarily sum terms vector product */
- register DataType * u; /* pointer to values used in elementary reflecting matrix */
- register DataType * y; /* pointer to array of dependent values */
- register DataType * pi; /* pointer to array of pi values from QR decomposition */
-
- HLock(Q->cells);
- HLock(P->cells);
- HLock(Y->cells);
-
- M = Q->cols;
- N = Q->rows;
- pi = *P->cells;
- y = *Y->cells;
-
- for(i=0; i<N; i++, pi++)
- { u = *Q->cells +i*M;
- sum = 0.0;
- for(k=i; k<M; k++)
- sum += u[k]*y[k];
- sum = sum/(*pi);
- for(k=i; k<M; k++)
- y[k] -= sum*u[k];
- }
-
- HUnlock(Q->cells);
- HUnlock(P->cells);
- HUnlock(Y->cells);
- }
-
- /*-------------------------
- Algorithm 3.1.3 of Stewart
- Solve linear system Rb=y for b, where R is an upper triangular nonsingular matrix.
- R is stored as its transpose so R(i,j) is at jth row, ith column.
- The diagonal terms are in seperate vector D as described in Algorithm 5.3.8 of
- Stewart. Answer is returned in vector D.
- */
- SolveRbY(R,D,Y)
- DTypeMatrix * R;
- DTypeVector * D; /* vector of diagonal terms for upper triangular matrix R */
- DTypeVector * Y; /* vector of dependent values in linear model */
- {
- DataType * r;
- register int i;
- register int j;
- register int M;
- register int N;
- register DataType * b; /* pointer to parameter values */
- register DataType * y; /* pointer to dependent values in linear model */
- register DataType sum;
- register DataType * temp;
-
- HLock(R->cells);
- HLock(D->cells);
- HLock(Y->cells);
-
- r = *R->cells;
- M = R->cols;
- N = D->rows; /* this also equals R->rows, is the number of parameters */
- b = *D->cells; /* use the diagonal vector to store parameter estimates */
- y = *Y->cells;
- for(i=N-1; i>-1; i--)
- { sum = 0.0;
- for(j=i+1, temp = r+i+j*M; j<N; j++, temp = temp + M)
- sum += (*temp)*b[j];
- b[i] = (y[i]-sum)/b[i];
- }
-
- HUnlock(R->cells);
- HUnlock(D->cells);
- HUnlock(Y->cells);
- }
-
- /*---------------------
- QR decomposition algorithm, see Algorithm 3.8 in
- Introduction to matrix Computations by G. Stewart, also Algorithm A3.2.1 in
- Numerical Methods for Unconstrained Optimization and Nonlinear Equations by
- Dennis and Shnabel. Used to solve linear system Ax=b, where A is (MxM), x is (Nx1).
-
- For coding efficiency the input matrix A is the transpose of the matrix given in
- the statement of the algorithm available in above references.
-
- Assumes more observations than parameters, ie M>N.
- */
-
- QRDecomposition(A,P,D)
- DTypeMatrix * A;
- DTypeVector * P, * D;
- {
- register int k, j, i;
- register int N; /* number of rows in A */
- register int M; /* number of columns in A */
- int sing = FALSE; /* flag for singular A matrix */
- DataType * kth_col; /* points to row of A, column of original untransposed matrix */
- DataType * pi; /* pointer to array for the pi values */
- register DataType * diag; /* pntr to array of diagonal terms for the triangular R matrix */
- register DataType * alpha; /* temporary pointer to cells in row pointed to by kth_col */
- register DataType * temp; /* temporary pointer */
- register DataType sign; /* sign of diagonal term in A matrix */
- register DataType aida, sigma, tau;
-
- HLock(A->cells);
- HLock(P->cells);
- HLock(D->cells);
-
- kth_col = *A->cells; /* start off in row zero */
- N = A->rows; /* number of parameters in linear system, since using transpose */
- M = A->cols; /* number of observations, since using transpose */
- pi = *P->cells;
- diag = *D->cells;
-
- for(k=0; k<N; k++, kth_col = kth_col + M, pi++, diag++)
- {
- aida = 0.0; /* initialize max abs value to zero */
- temp = kth_col+k;
- for(i=k; i<M; i++, temp++ )
- { tau = fabs(*temp); /* calculate aida */
- if(aida < tau) aida = tau; /* just borrowing tau */
- }
-
- if(aida == 0.0)
- { *pi = 0.0; /* column is already triangular */
- *diag = 0.0; /* see line 2.2T.2 of Alg A3.2.1 from Dennis and Schnabel */
- sing = TRUE;
- }
- else
- {
- for(i=k, alpha = kth_col + k; i<M; i++, alpha++)
- *alpha = (*alpha)/aida;
- sigma = 0.0;
- temp = kth_col+k;
- if(*temp>0) sign = 1; /* calculate sign term */
- else sign = -1;
-
- for(i=k; i<M; i++, temp++ ) /*------ */
- sigma += (*temp)*(*temp); /* calculate sigma */
- sigma = sign*sqrt(sigma); /*------ */
-
- *(kth_col + k) += sigma;
- *pi = (*(kth_col+k))*sigma;
- *diag = -aida*sigma;
-
- for(j=k+1; j<N; j++)
- {
- temp =kth_col+k; /*------ */
- alpha=kth_col+k + (j-k)*M; /* */
- tau = 0.0; /* calculate tau */
- for(i=k; i<M; i++, alpha++, temp++) /* */
- tau += (*temp)*(*alpha); /* */
- tau = tau/(*pi); /*------ */
-
- temp =kth_col+k;
- alpha=kth_col+k + (j-k)*M;
- for(i=k; i<M; i++, alpha++, temp++)
- *alpha -= (*temp)*tau;
- }
- }
- } /*end of for(k=0; k<N; k++, kth_col = kth_col + M, pi++, diag++)*/
- HUnlock(A->cells);
- HUnlock(P->cells);
- HUnlock(D->cells);
- return(sing);
- }
-
- WriteYXToFile(jac,vector,matrix)
- FILE * jac;
- DTypeVector * vector;
- DTypeMatrix * matrix;
- {
- int i,j;
- DataType * mcell;
- DataType * vcell;
-
- HLock(matrix->cells);
- HLock(vector->cells);
-
- mcell = *matrix->cells;
- vcell = *vector->cells;
- for(j=0; j<matrix->cols; j++)
- { fprintf(jac,"%.5lf ",vcell[j]);
- for(i=0; i<matrix->rows; i++)
- fprintf(jac,"%.8lf ",*(mcell + (i*matrix->cols + j)) );
- fprintf(jac,"\n");
- }
- HUnlock(matrix->cells);
- HUnlock(vector->cells);
- }
-